package com.springcloud.demo.servera.controller;

import com.springcloud.demo.base.vo.ResultVO;
import com.springcloud.demo.servera.server.IServerBFeign;
import com.springcloud.demo.servera.service.ServerAServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/server/a")
public class ServerAController {

    @Autowired
    private IServerBFeign serverBFeign;

    @Autowired
    private ServerAServiceImpl serverAService;

    @RequestMapping("/get")
    public ResultVO<String> get() {
        return ResultVO.ok("这是服务A");
    }

    @RequestMapping("/to/b")
    public ResultVO<String> toB() {
        return serverBFeign.get();
    }

    @RequestMapping("/insert")
    public ResultVO<String> insert() {
        serverAService.insert();
        return ResultVO.ok("这是服务A插入");
    }

    @RequestMapping("/seata")
    @GlobalTransactional(rollbackFor = Exception.class)
    public ResultVO<String> seata() {
        String xid = io.seata.core.context.RootContext.getXID();
        System.out.println(xid);
        serverAService.insert();
        serverBFeign.insert();
        return ResultVO.ok("seata测试");
    }
}
